/*
 * @Author: HanYaJun
 * @Date: 2023-01-11 10:33:11
 * @Email: hanyajun@wedobest.com.cn
 * @Description: 星消灭方块预制
 */

/**
 * @description: 方块类型
 */
export enum imageTypeEumn {
    /**蓝色 */
    BLUE = 1,
    /**绿色 */
    GREEN = 2,
    /**粉色 */
    PINK = 3,
    /**红色 */
    RED = 4,
    /**黄色 */
    YELLOW = 5
}

/**
 * @description: 方块颜色
 */
export class BlockColor {
    /**蓝色 */
    public static blueVal: string = '#00BFFF';
    /**绿色 */
    public static greenVal: string = '#00EE00';
    /**粉色 */
    public static pinkVal: string = '#FF00FF';
    /**红色 */
    public static redVal: string = '#CD2626';
    /**黄色 */
    public static yellowVal: string = '#FFFF00';
}

import { GTR } from '../../../Public/GTR';
import xxmGameEvents from '../Global/xxmGameEvents';
import { GameConfig } from './xxmPlayUI';
import LocalData from '../../../Public/game/gameData/LocalData';

let trace = function (...args) {
    GTR.logg.log("xxmItemBlock【HYJ】", ...args);
}
let traceError = function (...args) {
    GTR.logg.error("xxmItemBlock【HYJ】", ...args);
}

const { ccclass, property } = cc._decorator;
@ccclass
export class xxmItemBlock extends cc.Component {
    /**图片类型 */
    private imageType: number = null;
    /**二维坐标点 */
    private dCoordinates: number[] = [];
    /**设置锁定状态 */
    private lockState: boolean = false;
    /**移动时间 */
    private moveTiem: number = 0.1;
    /**设置摆放时的锁状态 */
    private createBolck: boolean = false;
    /**偏移量 */
    private offset: number = 0;

    /**
     * @description: 当附加到一个激活的节点上或者其节点第一次激活时候调用
     */
    protected onLoad(): void {
        // 递归查找自身或所有子节点中指定类型的组件
        let buttons: cc.Button[] = this.node.getComponentsInChildren(cc.Button);
        buttons.forEach((button) => {
            button.node.on('click', this.btnClickCallBack, this);
        });
    }

    /**
     * @description: 方块数据初始化
     */
    public initData(data: { position: cc.Vec3, colorValue: number }): void {
        this.imageType = data.colorValue;
        this.node.position = cc.v3(data.position.x, data.position.y);
        this.createBlockImg(data.colorValue);
    }

    /**
     * @description: 随机创建方块图片
     * @param {number} index
     * @return {*}
     */
    public createBlockImg(imageIndex: number): void {
        // 图片地址
        let pictureStr: string = `res_xxm/pub/image/play/block/card_${imageIndex}/spriteFrame`;
        // 方块图片加载
        GTR.res.load(pictureStr, cc.SpriteFrame, (error: any, spriteFrame: cc.SpriteFrame) => {
            if (!error) {
                let blockImageSprite: cc.Sprite = this.node.getComponent(cc.Sprite);
                blockImageSprite.spriteFrame = spriteFrame;
            } else {
                traceError('图片加载错误error:', error);
            }
        });
    }

    /**
     * @description: 点击回调
     */
    private btnClickCallBack(): void {
        GTR.event.dispatchEvent(xxmGameEvents.CLICK_BLOCK, { pot: this.dCoordinates });
        GTR.event.dispatchEvent(xxmGameEvents.CHANGE_COLOR, { pot: this.dCoordinates, imageType: this.imageType });
    }

    /**
     * @description: 设置图片类型
     * @param {imageTypeEumn} num
     */
    public setImageType(num: imageTypeEumn) {
        this.imageType = num;
    }

    /**
     * @description: 获取图片类型
     */
    public getImageType(): imageTypeEumn {
        return this.imageType;
    }

    /**
     * @description: 设置坐标点
     * @param {number} pot [r,c] 行, 列
     * @return {*}
     */
    public setDimensions(pot: number[]) {
        this.dCoordinates = pot;
        // 修改节点名称
        this.node.name = pot[0] + "_" + pot[1];
    }

    /**
     * @description: 获取坐标点
     * @return {*} 返回二维坐标点[r,c]
     */
    public getDimensions(): number[] {
        return this.dCoordinates;
    }

    /**
     * @description: 设置锁定状态
     * @param {boolean} lock 是否遍历过，已经遍历: true; 未遍历: false
     * @return {*}
     */
    public setBlockLock(lock: boolean) {
        this.lockState = lock;
    }

    /**
     * @description: 获取锁定状态
     * @return {*}
     */
    public getBlockLock() {
        return this.lockState;
    }

    /**
     * @description: 消除动画
     */
    public clearAnimation(): void {
        cc.tween(this.node)
            .to(this.moveTiem, { scale: cc.v3(0, 0, 0) })
            .call(() => {
                // 从父节点中删除该节点。 如果这个节点是一个孤立节点，那么什么都不会发生。
                this.node.removeFromParent();
                trace('正在消除方块');
            })
            .start();
    }

    /**
     * @description: 下移动画
     * @param {number} newPot
     */
    public moveDownAnimation(newPot: number[]): void {
        let x: number = GameConfig.size * (newPot[1] - this.dCoordinates[1]);
        let y: number = GameConfig.size * (newPot[0] - this.dCoordinates[0]);
        cc.tween(this.node)
            .by(this.moveTiem, { position: cc.v3(x, y, 0) })
            .call(() => {
                trace('正在计算下落');
            })
            .start();
        this.setDimensions(newPot);
    }

    /**
     * @description: 左移动画
     * @param {number} newPot
     */
    public moveLeftAnimation(newPot: number[]): void {
        let x: number = GameConfig.size * (newPot[0] - this.dCoordinates[0]);
        let y: number = GameConfig.size * (newPot[1] - this.dCoordinates[1]);
        cc.tween(this.node)
            .by(this.moveTiem, { position: cc.v3(y, x, 0) })
            .start()
        this.setDimensions(newPot);
    }

    /**
     * @description: 设置摆放时的锁状态
     * @param {boolean} block 是否遍历过，已经遍历: true; 未遍历: false
     */
    public setCreateBlock(block: boolean): void {
        this.createBolck = block;
    }

    /**
     * @description: 获取摆放时的锁状态
     * @return {*} 已经遍历: true; 未遍历: false
     */
    public getCreateBlock(): boolean {
        return this.createBolck;
    }

    /**设置偏移量 */
    public setOffset(offset: number): void {
        this.offset = offset;
    }

    /**获取偏移量 */
    public getOffset(): number {
        return this.offset;
    }

    /**
     * @description: 执行消除效果
     * @param {number} delay
     * @return {*}
     */
    public playClearAni(delay: number): void {
        cc.Tween.stopAllByTarget(this.node);
        cc.tween(this.node)
            .delay(delay)
            .to(0.1, { scale: cc.v3(0, 0, 0) })
            .call(() => {
                // 从父节点中删除该节点。 如果这个节点是一个孤立节点，那么什么都不会发生。
                this.node.removeFromParent;
            })
            .start()
    }

    /**
     * @description: 当该组件被销毁时调用
     */
    protected onDestroy(): void {
        this.dCoordinates = [];
    }
}

